<?php

class Admin_AuthController extends Zend_Controller_Action{
    
    private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();                
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
                 
        //$this->_auth        = $this->_acl->checkAllowed();                
        //$this->view->auth   = $this->_auth;
//        $config = new Zend_Config_Ini(CONFIG_PATH . '/application.ini', APPLICATION_ENV);               
        //$layout = Zend_Layout::getMvcInstance();
        //$layout->disableLayout();        
    }
    public function loginAction ()
    {                          
        $login_form = new Admin_Form_Login();
        $this->view->form = $login_form;                                    
        if ($this->_request->isPost()){                        
            $form_data = $this->_request->getPost();
            if($login_form->isValid($form_data)){                
                $adapter = new Zend_Auth_Adapter_DbTable(NULL,'admin','username','password','MD5(?)');                        
                $uname = $this->_request->getParam('username');
                $paswd = $this->_request->getParam('password');                        
                $adapter->setIdentity($uname);
                $adapter->setCredential($paswd);
                $auth = Zend_Auth::getInstance();
                $storage = new Zend_Auth_Storage_Session(SESSION_NAMESPACE);
                $ss=new Zend_Session_Namespace($storage->getNamespace());                                
                $ss->setExpirationSeconds(3600);                
                $auth->setStorage($storage);                                                 
                $result = $auth->authenticate($adapter);                               
                if ($result->isValid()) {
                    $modelAdmin = new Admin_Model_Admin();                                                           
                    $userCheck = $modelAdmin->getDataByUsername($uname);                                                                                                                                          
                    if($userCheck->status == 1){
                        $modelAdmin->updateSession(session_id(),$userCheck['adminId']);
                        $auth->getStorage()->write(array('username'=>$userCheck['username'],'adminId'=>$userCheck['adminId'],'fullName'=>$userCheck['fullName']));                                                                                                                                                                                                                     
                        $this->_helper->redirector('index','dashboard', 'admin');
                    }else{                        
                        $auth->clearIdentity();
                        $this->view->mess = array('Tài khoản của bạn đang tạm khóa!');
                        $this->view->flag = 'alert';
                        $this->view->form = $login_form;
                    }                                                                                                                        
                }else{
                    //$this->_redirect('admin/auth/login');
                    $this->_helper->redirector('login','auth', 'admin');
                    $this->view->mess = array('Username hoặc mật khẩu không đúng!');
                    $this->view->flag = 'error';
                    $this->view->form = $login_form;                                               
                }
            }                                                            
        }      
    }    
    public function logoutAction(){                                                
        $auth=Zend_Auth::getInstance();
        $auth->clearIdentity();              
        $this->_redirect('admin/auth/login');
    }   
}



